'use strict';
const db = uniCloud.database();
const Response = require('response');
const CheckParams = require('check_params');
//增加浏览量
async function addViews(data){
	let row =await db.collection('opendb-news-articles').field({view_count:!0}).where({_id:data.id}).get();
	row = row.data;
	let view_count = Number(row[0].view_count||0) + 1;
	let resp =  await db.collection('opendb-news-articles').where({_id:data.id}).update({
			view_count:view_count
	})
	return Response({type:'success'});
}
//用户收藏
async function userCollect(data){
	/* 
	 data={id://文章标题，uid://用户id待定}
	 */
	let like_count =data.isLike?0:1;
	let resp =  await db.collection('opendb-news-articles').where({_id:data.id}).update({
			like_count:like_count
	})
	return Response({type:'success'});
}




exports.main = async (event, context) => {
	let type = event.type, res = {};
	switch(type){
		//增加浏览量
		case("views"):res = addViews(event.data);break;
		//用户收藏
		case("collect"):res = userCollect(event.data);break;
		default:res=Response({type:"success"});break;
	}
	
	//返回数据给客户端
	return res
};
